package com.example.demo.controller;

import com.example.demo.bean.Car;
import com.example.demo.bean.HttpResult;
import com.example.demo.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    CarService carService;
    @RequestMapping("/queryCar")
    public HttpResult<List<Car>> queryCar(){
        HttpResult<List<Car>> result = new HttpResult<>();
        List<Car> car = carService.queryCar();
        result.setData(car);
        return result;
    }
    @RequestMapping("/insertCar")
    public HttpResult<Boolean> insertCar(@RequestBody Car car){
        HttpResult<Boolean> result = new HttpResult<>();
        Car car1 = carService.queryGoods(car);
        if(car1 == null) {
            int i = carService.insertCar(car);
            if (i == 1) {
                result.setData(true);
            } else {
                result.setData(false);
            }
        }else if(car1!=null){
            car1.setNum(car.getNum()+car1.getNum());
            int j = carService.updateCar(car1);
            if(j == 1){
                result.setData(true);
            }else {
                result.setData(false);
            }
        }
        return result;
    }
    @RequestMapping("/updateCar")
    public HttpResult<Boolean> updateCar(@RequestBody Car car){
        HttpResult<Boolean> result = new HttpResult<>();
        int i = carService.updateCar(car);
        if(i == 1) {
            result.setData(true);
        }else {
            result.setData(false);
        }
        return result;
    }
    @RequestMapping("/delCar")
    public HttpResult<Boolean> delCar(int id){
        HttpResult<Boolean> result = new HttpResult<>();
        int i = carService.delCar(id);
        if(i == 1) {
            result.setData(true);
        }else {
            result.setData(false);
        }
        return result;
    }
}
